package com.ss.android.messagebus;

import android.util.Log;
import com.ss.android.messagebus.a.c;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CopyOnWriteArrayList;

public final class SubscriptionRouter {
    private static final bb a = bb.getInstance();

    public static void addSubscriber(Object obj) {
        CopyOnWriteArrayList v0_6;
        bb v6;
        try {
            v6 = SubscriptionRouter.a;
            if(obj == null) {
                return;
            }

            synchronized (v6) {

                f v7 = v6.c;
                if (v7.mSubcriptionMap == null) {
                    Log.d("SubscriptionRouter", "the mSubcriptionMap is null.");
                } else {
                    Class v5;
                    for (v5 = obj.getClass(); v5 != null; v5 = v5.getSuperclass()) {
                        String v0_2 = v5.getName();
                        int v0_3 = (v0_2.startsWith("java.")) || (v0_2.startsWith("javax.")) || (v0_2.startsWith("android.")) ? 1 : 0;
                        if (v0_3 != 0) {
                            break;
                        }

                        Method[] v8 = v5.getDeclaredMethods();
                        int v1;
                        for (v1 = 0; v1 < v8.length; ++v1) {
                            Method v9 = v8[v1];
                            Annotation v0_4 = v9.getAnnotation(Subscriber.class);
                            if (v0_4 != null) {
                                Class[] v3 = v9.getParameterTypes();
                                if (v3 != null && v3.length == 1) {
                                    Class v3_1 = v3[0];
                                    if (v3_1.equals(Boolean.TYPE)) {
                                        v3_1 = Boolean.class;
                                    } else if (v3_1.equals(Integer.TYPE)) {
                                        v3_1 = Integer.class;
                                    } else if (v3_1.equals(Float.TYPE)) {
                                        v3_1 = Float.class;
                                    } else if (v3_1.equals(Double.TYPE)) {
                                        v3_1 = Double.class;
                                    }

                                    MessageType v10 = new MessageType(v3_1, ((Subscriber) v0_4).a());
                                    g v3_2 = new g(v9, v10, ((Subscriber) v0_4).b());
                                    v0_6 = (CopyOnWriteArrayList) v7.mSubcriptionMap.get(v10);
                                    if (v0_6 == null) {
                                        v0_6 = new CopyOnWriteArrayList();
                                    }

                                    e v9_1 = new e(obj, v3_2);
                                    if (v0_6.contains(v9_1)) {
                                        continue;
                                    }

                                    v0_6.add(v9_1);
                                    v7.mSubcriptionMap.put(v10, v0_6);
                                }
                            }
                        }
                    }
                }
            }
            return;
        }
        catch(Throwable v0_1) {
            v0_1.printStackTrace();
        }
    }

    public static void removeSubscriber(Object arg9) {
        bb v2;
        try {
            v2 = SubscriptionRouter.a;
            if(arg9 == null) {
                return;
            }

            synchronized (v2) {
                Iterator v3 = v2.c.mSubcriptionMap.values().iterator();
                LinkedList v4 = new LinkedList();
                while (v3.hasNext()) {
                    Object v0_2 = v3.next();
                    if (v0_2 != null) {
                        Iterator v5 = ((CopyOnWriteArrayList) v0_2).iterator();
                        while (v5.hasNext()) {
                            Object v1 = v5.next();
                            Object v6 = ((e) v1).a.get();
                            if (v6 == null) {
                                continue;
                            }

                            if (!v6.equals(arg9)) {
                                continue;
                            }
                            Log.d("SubscriptionRouter", "remove subscriber: " + arg9.getClass().getName());
                            ((List) v4).add(v1);
                        }

                        ((CopyOnWriteArrayList) v0_2).removeAll(((Collection) v4));
                        ((List) v4).clear();
                    }

                    if (v0_2 != null && ((CopyOnWriteArrayList) v0_2).size() != 0) {
                        continue;
                    }

                    v3.remove();
                }
            }
        } catch(Exception v0) {
            v0.printStackTrace();
        }
    }

    public static void c(Object paramClass) {
        c v2_2;
        Object v1_1;
        bb v1 = SubscriptionRouter.a;
        ((Queue)v1.a.get())
                .offer(new MessageType(paramClass.getClass(), "default_tag"));
        com.ss.android.messagebus.bb.a v4 = v1.b;
        Object v0 = v4.f.a.get();
        do {
            if(((Queue)v0).size() <= 0) {
                return;
            }

            v1_1 = ((Queue)v0).poll();
            if(v4.d.containsKey(v1_1)) {
                v1_1 = v4.d.get(v1_1);
            } else {
                com.ss.android.messagebus.b.a v5 = v4.e;
                Class clazz = paramClass.getClass();
                LinkedList v2 = new LinkedList();
                while(clazz != null) {
                    ((List)v2).add(new MessageType(clazz, ((MessageType)v1_1).tag));
                    v5.a(((List)v2), clazz, ((MessageType)v1_1).tag);
                    clazz = clazz.getSuperclass();
                }

                v4.d.put(v1_1, v2);
                v1_1 = v2;
            }

            if(v1_1 != null){
                Iterator v3_1 = ((List)v1_1).iterator();
                do {
                    if(!v3_1.hasNext()) {
                        break;
                    }
                    v1_1 = bb.a(v4.f).get(v3_1.next());
                    if(v1_1 != null){
                        Iterator v5_1 = ((List)v1_1).iterator();
                        while(true) {
                            if(!v5_1.hasNext()) {
                                break;
                            }
                            v1_1 = v5_1.next();
                            ThreadMode v2_1 = ((e)v1_1).c;
                            if(v2_1 == ThreadMode.ASYNC) {
                                v2_2 = v4.c;
                            }
                            else if(v2_1 == ThreadMode.CURRENT) {
                                v2_2 = v4.b;
                            }
                            else {
                                v2_2 = v4.a;
                            }
                            v2_2.a(((e)v1_1), paramClass);
                        }
                    }
                } while(true);
            }
        } while(true);
    }
}

